package com.atguigu.sparkcore.day02.kv

import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}

/**
 * Author atguigu
 * Date 2020/10/28 10:14
 */
object PartitionByDemo {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf().setAppName("PartitionByDemo").setMaster("local[2]")
        val sc: SparkContext = new SparkContext(conf)
        val list1 = List(1 -> "a", 2 -> "b", 3 -> "c")
        val rdd1 = sc.parallelize(list1, 2)
        val rdd2 = rdd1
            .map {
                case (k, v) => v -> (k -> v)
            }
            .partitionBy(new HashPartitioner(3))
            .map(_._2)
        val rdd3 = rdd2.mapPartitionsWithIndex((i, it) => {
            it.map((i, _))
        })
        rdd3.collect.foreach(println)
        sc.stop()
        
    }
}

/*
kv 形式的rdd, 键值对形式


 */
